package liy.demo.wikiproject.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import liy.demo.wikiproject.domain.Content;
import liy.demo.wikiproject.domain.Doc;
import liy.demo.wikiproject.mapper.ContentMapper;
import liy.demo.wikiproject.mapper.DocMapper;
import liy.demo.wikiproject.req.DocQueryReq;
import liy.demo.wikiproject.req.DocSaveReq;
import liy.demo.wikiproject.resp.DocResp;
import liy.demo.wikiproject.resp.CommonResp;
import liy.demo.wikiproject.resp.PageResp;
import liy.demo.wikiproject.util.CopyUtil;
import liy.demo.wikiproject.util.SnowFlake;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DocService {

    @Resource
    DocMapper DocMapper;
    @Resource
    ContentMapper ContentMapper;

    @Resource
    SnowFlake snowFlake;
    public List<DocResp> all(DocQueryReq req) {
        List<Doc> categories = null;
        if(req.getEbookId()!=null) {
            categories= DocMapper.selectByEbookId(req.getEbookId());
        }else{
            categories = DocMapper.selectAll();
        }
        List<DocResp> respList = CopyUtil.copyList(categories, DocResp.class);
        return buildTree(respList);
    }
    public void addVote(DocQueryReq req) {
        ContentMapper.updateVote(req.getId());

    }

    public void addView(DocQueryReq req) {
        ContentMapper.updateView(req.getId());
    }
    public DocResp findConent(DocQueryReq req) {
        Content content = ContentMapper.selectByPrimaryKey(req.getId());
        Doc doc = DocMapper.selectByPrimaryKey(req.getId());
        DocResp copy = CopyUtil.copy(doc, DocResp.class);
        copy.setContent(content.getContent());
        return copy;
    }
    private List<DocResp> buildTree(List<DocResp> categories) {
        //首先找到所有parent为0的节点作为根节点
        List<DocResp> rootCategories = categories.stream()
                .filter(doc->doc.getParent()==0)
                .collect(Collectors.toList());
        //然后对于每一个根节点，挨个建树
        for(DocResp rootDoc: rootCategories) {
            buildSubTree(rootDoc,categories);
        }
        return   rootCategories;
    }
    private void buildSubTree(DocResp parent, List<DocResp> categories) {

        List<DocResp> children = categories.stream()
                .filter(doc-> Objects.equals(doc.getParent(), parent.getId()))
                .collect(Collectors.toList());
        parent.setChildren(children);
        for (DocResp child : children) {
            buildSubTree(child, categories);
        }
    }


    public PageResp<DocResp> list( DocQueryReq req) {
        List<Doc> docList;
        docList = DocMapper.selectAll();
        List<DocResp> respList = CopyUtil.copyList(docList, DocResp.class);
        PageInfo<Doc> pageInfo = new PageInfo<>(docList);
        PageResp<DocResp> pageResp = new PageResp<>();
        pageResp.setTotal((int) pageInfo.getTotal());
        pageResp.setList(respList);
        return pageResp;
    }


    public CommonResp<String> delete(DocQueryReq req) {
        long id = req.getId();
        CommonResp<String>commonResp = new CommonResp<>();
        try {
            DocMapper.deleteByPrimaryKey(id);
            commonResp.setContent("删除成功");
        }catch (Exception e) {
            commonResp.setContent("删除失败");
        }finally {
            return commonResp;
        }
    }
    public void save(DocSaveReq req) {
        Doc doc = CopyUtil.copy(req,Doc.class);
        Content content = CopyUtil.copy(req, Content.class);
        if(ObjectUtils.isEmpty(doc.getId())) {
            long l = snowFlake.nextId();
            doc.setId(l);
            doc.setParent(0L);
            DocMapper.insert(doc);
            content.setId(l);
            ContentMapper.insert(content);
        }else{
            DocMapper.updateByPrimaryKey(doc);
            ContentMapper.updateByPrimaryKey(content);
        }
    }
}
